﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TLSharp
{
    public class TouchLeagueService
    {
        public static string ServiceURL = System.Configuration.ConfigurationSettings.AppSettings["TLServiceURL"];

        public static BrowseMyGameResponse BrowseGames(string Username, string Password, string ApplicationGUID)
        {
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}", Username, Password, ApplicationGUID);

            string Response = GetResponseString(ServiceURL + "BrowseMyGames.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            BrowseMyGameResponse b = (BrowseMyGameResponse)s.Deserialize(new JsonTokenReader(o), typeof(BrowseMyGameResponse));

            return b;
        }
        public static BrowseLadderResponse BrowseLadder(string Username, string Password, string ApplicationGUID,double LadderStartRank)
        {
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&LadderStartRank={3}", Username, Password, ApplicationGUID, LadderStartRank.ToString());

            string Response = GetResponseString(ServiceURL + "BrowseLadder.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            BrowseLadderResponse b = (BrowseLadderResponse)s.Deserialize(new JsonTokenReader(o), typeof(BrowseLadderResponse));

            return b;
        }


        public static RequestMatchResponse RequestMatchTargeted(string Username, string Password, string ApplicationGUID,int TargetTeamID,bool bChallenge)
        {
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&TargetTeamID={3}&bChallenge={4}", Username, Password, ApplicationGUID,TargetTeamID,bChallenge);

            string Response = GetResponseString(ServiceURL + "RequestMatch.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            RequestMatchResponse b = (RequestMatchResponse)s.Deserialize(new JsonTokenReader(o), typeof(RequestMatchResponse));

            return b;
        }

        public static RequestMatchResponse RequestMatchRank(string Username, string Password, string ApplicationGUID, int MinRank,int MaxRank, bool bChallenge)
        {
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&MinRank={3}&bChallenge={4}&MaxRank={5}", Username, Password, ApplicationGUID, MinRank , bChallenge,MaxRank );

            string Response = GetResponseString(ServiceURL + "RequestMatch.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            RequestMatchResponse b = (RequestMatchResponse)s.Deserialize(new JsonTokenReader(o), typeof(RequestMatchResponse));

            return b;
        }

        public static GetUserProfileResponse GetUserProfile(string Username, string Password, string ApplicationGUID)
        {
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}", Username, Password, ApplicationGUID);

            string Response = GetResponseString(ServiceURL + "GetUserProfile.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            GetUserProfileResponse b = (GetUserProfileResponse)s.Deserialize(new JsonTokenReader(o), typeof(GetUserProfileResponse));

            return b;
        }

        public static GetTurnStateResponse GetTurnState(string Username, string Password, string ApplicationGUID,int GameID)
        {
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&GameID={3}", Username, Password, ApplicationGUID,GameID);

            string Response = GetResponseString(ServiceURL + "GetTurnState.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            GetTurnStateResponse b = (GetTurnStateResponse)s.Deserialize(new JsonTokenReader(o), typeof(GetTurnStateResponse));

            return b;
        }

        public static SetTurnStateResponse SetTurnState(string Username, string Password, string ApplicationGUID, int GameID, int NextTurnTeamID, string NewGameState,string ScoreArray,bool bFinalize)
        {
            
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&GameID={3}&NextTurnTeamID={4}&NewGameState={5}&ScoreArray={6}&bFinalize={7}", Username, Password, ApplicationGUID, GameID,NextTurnTeamID,NewGameState,ScoreArray,bFinalize);

            string Response = GetResponseString(ServiceURL + "SetTurnState.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            SetTurnStateResponse b = (SetTurnStateResponse)s.Deserialize(new JsonTokenReader(o), typeof(SetTurnStateResponse));

            return b;
        }

        public static ChallengeNonUserResponse ChallengeNonUser(string Username, string Password, string ApplicationGUID, string FriendEmail, string MyName)
        {

            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&FriendEmail={3}&MyName={4}", Username, Password, ApplicationGUID, FriendEmail, MyName);

            string Response = GetResponseString(ServiceURL + "ChallengeNonUser.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            ChallengeNonUserResponse b = (ChallengeNonUserResponse)s.Deserialize(new JsonTokenReader(o), typeof(ChallengeNonUserResponse));

            return b;
        }


        public static RemoveRequestResponse RemoveRequest(string Username, string Password, string ApplicationGUID, int GameRequestID)
        {
            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&GameRequestID={3}", Username, Password, ApplicationGUID, GameRequestID);

            string Response = GetResponseString(ServiceURL + "RemoveRequest.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            RemoveRequestResponse b = (RemoveRequestResponse)s.Deserialize(new JsonTokenReader(o), typeof(RemoveRequestResponse));

            return b;
        }

        public static SetGameNoteResponse SetGameNote(string Username, string Password, string ApplicationGUID, int GameID, int ReplyToNoteID, string Note, bool bPrivate, string PrivateTeamList, string RetrievalLink)
        {

            Dictionary<string, string> Results = new Dictionary<string, string>();
            string poststring = String.Format("Username={0}&Password={1}&ApplicationGUID={2}&GameID={3}&ReplyToNoteID={4}&Note={5}&RetrievalLink={6}&bPrivate={7}&PrivateTeamList={8}", Username, Password, ApplicationGUID, GameID.ToString(), ReplyToNoteID.ToString() , Note, RetrievalLink , bPrivate.ToString(),PrivateTeamList  );

            string Response = GetResponseString(ServiceURL + "SetGameNote.aspx", poststring);

            Newtonsoft.Json.Linq.JObject o = JObject.Parse(Response);

            JsonSerializer s = new JsonSerializer();

            SetGameNoteResponse b = (SetGameNoteResponse)s.Deserialize(new JsonTokenReader(o), typeof(SetGameNoteResponse));

            return b;
        }

        public static string CreateJson(object o)
        {
            return JavaScriptConvert.SerializeObject(o);
        }

        public static string GetResponseString(string url, string poststring)
        {
            
            HttpWebRequest httpRequest =
            (HttpWebRequest)WebRequest.Create(url);

            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            


            byte[] bytedata = Encoding.UTF8.GetBytes(poststring);
            httpRequest.ContentLength = bytedata.Length;

            Stream requestStream = httpRequest.GetRequestStream();
            requestStream.Write(bytedata, 0, bytedata.Length);
            requestStream.Close();


            HttpWebResponse httpWebResponse =
            (HttpWebResponse)httpRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();

            StringBuilder sb = new StringBuilder();

            using (StreamReader reader =
            new StreamReader(responseStream, System.Text.Encoding.UTF8))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    sb.Append(line);
                }
            }

            return sb.ToString();
        }

    }


}
